<!--
 * @Description: 
 * @Date: 2023-04-01 23:44:42
 * @LastEditTime: 2023-04-06 21:53:26
 * @Author: Black
 * @FilePath: \store_admin\src\components\ImageAside.vue
-->
<template>
  <el-aside width="300px" class="image_aside" v-loading="loading">

    <div class="top">
      <AsideList v-for="(item, index) in list" :key="index" :active="activeId === item.id" @edit="handleEdit(item)"
        @delete="handleDelete(item.id)" @click="handleChangeActiveId(item.id)">
        {{ item.name }}
      </AsideList>
    </div>

    <div class="bottom">
      <el-button class="btn1" type="primary" @click="handleUpload">上传图片</el-button>
      <el-pagination background layout="prev, next" :total="total" :current-page="currentPage" :page-size="limit"
        @current-change="getImageData" />
      <el-button class=btn2 type="primary" @click="handleCreate">新增分类</el-button>
    </div>

  </el-aside>

  <FormDrawer :title="drawerTitle" ref="formDrawerRef" @submit="handleSubmit">
    <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input-number v-model="form.order" :min="0" :max="1000"></el-input-number>
      </el-form-item>
    </el-form>
  </FormDrawer>

</template>

<script setup>
import AsideList from './AsideList.vue';
import {
  getImageClassList,
  createImageClass,
  updateImageClass,
  deleteImageClass
} from '~/api/image_class.js';
import { ref, reactive } from 'vue';
import FormDrawer from './FormDrawer.vue';
import { toast } from "~/composables/util.js"
import { computed } from '@vue/reactivity';

// 加载动画
const loading = ref(false)
const list = ref([])
const activeId = ref(0)

// 分页
const currentPage = ref(1)
const total = ref(0)
const limit = ref(9)

// 获取数据
function getImageData(p = null) {
  if (typeof p == "number") {
    currentPage.value = p
  }
  loading.value = true
  getImageClassList(currentPage.value)
    .then(res => {
      total.value = res.totalCount
      list.value = res.list
      let item = list.value[0]
      if (item) {
        handleChangeActiveId(item.id)
      }
    })
    .finally(() => {
      loading.value = false
    })
}
getImageData()

const editId = ref(0)
const drawerTitle = computed(() => editId.value ? "修改" : "新增")
const formDrawerRef = ref(null)
const formRef = ref(null)

// 表单验证
const form = reactive({
  name: "",
  order: 50
})

const rules = {
  name: [{
    required: true,
    message: '图库分类名称不能为空',
    trigger: 'blur'
  }]
}

// 提交
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) return
    formDrawerRef.value.showLoading()
    const fun = editId.value ? updateImageClass(editId.value, form) : createImageClass(form)
    fun.then(res => {
      toast(drawerTitle.value + "成功")
      getImageData(editId.value ? currentPage.value : 1)
      formDrawerRef.value.close()
    }).finally(() => {
      formDrawerRef.value.hideLoading()
    })
  })
}

// 新增
const handleCreate = () => {
  editId.value = 0
  form.name = ""
  form.order = 50
  formDrawerRef.value.open()
}

// 编辑
const handleEdit = (row) => {
  // console.log(row)
  editId.value = row.id
  form.name = row.name
  form.order = row.order
  formDrawerRef.value.open()
}

// 删除
const handleDelete = (id) => {
  loading.value = true
  deleteImageClass(id).then(res => {
    toast("删除成功")
    getImageData()
  }).finally(() => {
    loading.value = false
  })
}

const emit = defineEmits(["change","upload"])

// 选中图库分类 id
function handleChangeActiveId(id) {
  activeId.value = id
  emit("change", id)
}

const handleUpload = () => {
  // 调用父组件的upload方法
  emit("upload")
}

</script>

<style scoped>
.image_aside {
  border-left: 1px solid #eeeeee;
  position: relative;
}

.image_aside .top {
  position: absolute;
  width: 100%;
  top: 0px;
  right: 0;
  bottom: 50px;
  overflow-y: auto;
}

.image_aside .bottom {
  position: absolute;
  bottom: 5px;
  height: 50px;
  left: 0;
  right: 0;
  @apply flex items-center justify-center;
}

.btn2 {
  @apply flex items-center bg-violet-300 text-light-50;
  position: absolute;
  height: 34px;
  right: 10px;
  border-radius: 10px;
  border: none;
  color: #fff;
  cursor: pointer;
}

.btn2:hover {
  box-shadow: 0 0 5px rgba(118, 56, 176, 0.5);
}

.btn1 {
  @apply flex items-center bg-rose-200 text-light-50;
  position: absolute;
  height: 34px;
  left: 10px;
  border-radius: 10px;
  border: none;
  color: #fff;
  cursor: pointer;
}

.btn1:hover {
  box-shadow: 0 0 5px rgba(241, 181, 181, 0.5);
}
</style>